<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    字符串处理函数 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						字符串处理函数
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/11 23:00 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<h2><a id="%E9%9C%80%E8%A6%81%E5%BC%95%E5%85%A5%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>需要引入：</h2>
<pre class="line-numbers"><code class="language-c">#include &lt;string.h&gt;
</code></pre>
<h3><a id="strcat%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>strcat()：</h3>
<pre class="line-numbers"><code class="language-c">strcat(字符数组1, 字符数组2);
</code></pre>
<ul>
<li>连接两个字符数组中的字符串，把字符串2接到字符串1的后面</li>
</ul>
<h3><a id="strcpy%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>strcpy()：</h3>
<pre class="line-numbers"><code class="language-c">strcpy(字符数组1, 字符串2);
</code></pre>
<ul>
<li>将<code>字符串2</code>复制到<code>字符数组1</code>中；</li>
<li><code>字符数组1</code>的长度不应小于<code>字符串2</code>的长度；</li>
<li><code>字符数组1</code>必须写成数组名形式（如<code>str1</code>），<code>字符串2</code>可以使字符数组名或字符串常量，例如：</li>
</ul>
<pre class="line-numbers"><code class="language-c">strcpy(str1,&quot;Hello&quot;)；
</code></pre>
<ul>
<li>可以用<code>strncpy()</code>函数将<code>字符串2</code>中的前面若干字符复制到<code>字符数组1</code>中，例如：</li>
</ul>
<pre class="line-numbers"><code class="language-c">strncpy(str1,str2,2)；
</code></pre>
<h3><a id="strcmp%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>strcmp()：</h3>
<pre class="line-numbers"><code class="language-c">strcmp(字符串1, 字符串2);
</code></pre>
<ul>
<li>比较<code>字符串1</code>和<code>字符串2</code>（依次比较ASCII编码）；</li>
<li>如果<code>字符串1==字符串2</code>，则函数值为0；</li>
<li>如果<code>字符串1&gt;字符串2</code>，则函数值为一个正整数；</li>
<li>如果<code>字符串1&lt;字符串2</code>，则函数值为一个负整数。</li>
</ul>
<p><strong>注意：</strong></p>
<ul>
<li>不能用赋值语句将一个字符串常量或字符数组直接赋值给一个字符数组，例如：</li>
</ul>
<pre class="line-numbers"><code class="language-c">char str1[10], str2[10];
str1 = “Hello”;
str1 = str2;
</code></pre>
<p><strong>注意：</strong></p>
<ul>
<li>只能用<code>strcpy()</code>函数将一个字符串复制到另一个字符数组中，赋值语句只能将一个字符赋给一个字符型变量或字符数组元素，例如：</li>
</ul>
<pre class="line-numbers"><code class="language-c">char a[5], c1, c2;
c1 = ‘A’;
c2 = ‘B’;
a[0] = ‘C’;
</code></pre>
<h3><a id="strlen%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>strlen()：</h3>
<pre class="line-numbers"><code class="language-c">strlen(字符数组);
</code></pre>
<ul>
<li>测试字符串长度（输出\0之前的字符个数）</li>
</ul>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>